package com.zhihao.domain.strategy.repository;

import com.zhihao.domain.strategy.model.entity.StrategyAwardEntity;
import com.zhihao.domain.strategy.model.entity.StrategyEntity;
import com.zhihao.domain.strategy.model.entity.StrategyRuleEntity;
import com.zhihao.domain.strategy.model.valobj.RuleTreeVO;
import com.zhihao.domain.strategy.model.valobj.RuleWeightVO;
import com.zhihao.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import com.zhihao.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.checkerframework.checker.units.qual.K;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Jackson
 * @description 策略服务仓储接口
 * @create 2023-12-23 09:33
 */
public interface IStrategyRepository {

    /* 查询指定ID的抽奖策略(strategy-award 1-n) */
    List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId);

    /* 将抽奖策略的概率范围和概率查找表存储到 Redis*/
    <K, V> void storeStrategyAwardSearchRateTable(String strategyKey, Integer raffleRandomNumRange, Map<K, V> strategyAwardSearchRateTable);

    <K, V> Map<K, V> getMap(String key);

    /* 根据生成的rateKey随机数，获取到strategyId对应的奖品awardId */
    Integer getStrategyAwardAssemble(String strategyKey, Integer rateKey);

    /* 获取 执行抽奖使用的随机数范围 raffleRandomNumRange */
    int getRateRange(Long strategyId);
    int getRateRange(String strategyKey);

    /* 查询策略的实体类对象 */
    StrategyEntity queryStrategyEntityByStrategyId(Long strategyId);

    /* 查询策略规则rule_model的rule_value*/
    StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel);

    /* 查询策略规则值 */
    String queryStrategyRuleValue(Long strategyId, String ruleModel);
    String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel);

    /* 查询‘奖品’包含的规则rule_value */
    StrategyAwardRuleModelVO queryStrategyAwardRuleModelVO(Long strategyId, Integer awardId);

    /*
        规则树的构造(依赖于相关的DAO)
     */
    RuleTreeVO queryRuleTreeVOByTreeId(String treeId);

    /* 策略奖品库存信息缓存至redis */
    void cacheStrategyAwardCount(String cacheKey, Integer awardCount);

    /* 扣减redis缓存中的库存 */
    Boolean subtractionAwardStock(String cacheKey);
    Boolean subtractionAwardStock(String cacheKey, Date endDateTime);
    /* 奖品库存扣减 消息队列 */
    void awardStockConsumeSendQueue(StrategyAwardStockKeyVO strategyAwardStockKeyVO);

    /* 获取库存扣减队列的信息 */
    StrategyAwardStockKeyVO takeQueueValue();

    /* 更新奖品库存的数据库接口 */
    void updateStrategyAwardStock(Long strategyId, Integer awardId);

    /* 查询抽奖结果实体 */
    StrategyAwardEntity queryStrategyAwardEntity(Long strategyId, Integer awardId);

    /* 活动id所装配的策略id----一对一关系 */
    Long queryStrategyIdByActivityId(Long activityId);

    /**
     * 查询用户抽奖次数 - 当天的；策略ID:活动ID 1:1 的配置，可以直接用 strategyId 查询。
     *
     * @param userId     用户ID
     * @param strategyId 策略ID
     * @return 用户今日参与次数
     */
    Integer queryTodayUserRaffleCount(String userId, Long strategyId);

    /* 查询奖品‘锁规则’的值 */
    Map<String, Integer> queryAwardRuleLockCount(String[] treeIds);

    /**
     * 根据用户ID、策略ID，查询用户活动账户总使用量
     *
     * @param userId     用户ID
     * @param strategyId 策略ID
     * @return 使用总量
     */
    Integer queryActivityAccountTotalUseCount(String userId, Long strategyId);

    /**
     * 查询奖品权重配置
     *
     * @param strategyId 策略ID
     * @return 权重规则
     */
    List<RuleWeightVO> queryAwardRuleWeight(Long strategyId);


    /**
     * 查询有效活动的奖品配置
     *
     * @return 奖品配置列表
     */
    List<StrategyAwardStockKeyVO> queryOpenActivityStrategyAwardList();

    /**
     * 存储抽奖策略对应的Bean算法
     *
     * @param key      策略ID
     * @param beanName 策略对象名称
     */
    void cacheStrategyArmoryAlgorithm(String key, String beanName);

    /**
     * 获取存储抽奖策略对应的Bean算法
     *
     * @param key 策略ID
     * @return 策略对象名称
     */
    String queryStrategyArmoryAlgorithmFromCache(String key);

}
